/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.UserCertDTO;
import org.springblade.modules.admin.entity.UserCert;
import org.springblade.modules.admin.enums.CertStatusEnum;
import org.springblade.modules.admin.service.UserCertService;
import org.springblade.modules.admin.vo.UserCertVO;
import org.springblade.modules.admin.vo.cert.UserCertStatisticsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 证件操作记录表 控制器
 *
 * @author ysq
 * @since 2024-03-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/user/cert")
@Api(value = "证件表", tags = "证件接口")
public class UserCertController extends BladeController {

	private final UserCertService userCertService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R<UserCertVO> detail(@ApiParam(value = "主键id", required = true) @RequestParam Long id) {
		UserCertVO detail = userCertService.detail(id);
		return R.data(detail);
	}

	/**
	 * 自定义分页 证件操作记录表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<UserCertVO>> page(UserCertDTO userCert, Query query) {
		IPage<UserCertVO> pages = userCertService.selectUserCertPage(Condition.getPage(query), userCert);
		return R.data(pages);
	}

	/**
	 * 新增 证件操作记录表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增")
	public R save(@Valid @RequestBody UserCert userCert) {
		return R.status(userCertService.insert(userCert));
	}

	/**
	 * 修改 证件操作记录表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改")
	public R update(@Valid @RequestBody UserCert userCert) {
		return R.status(userCertService.update(userCert));
	}

	/**
	 * 新增或修改 证件操作记录表
	 */
	@PostMapping("/batch/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "批量更新")
	public R batchUpdate(@RequestBody List<UserCert> userCerts) {
		return R.status(userCertService.batchUpdate(userCerts));
	}

	@PostMapping("/check/cert/uniqueness")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "检验证件的唯一性")
	public R delWorkCheck(@Valid @RequestBody UserCert userCert) {
		return R.data(userCertService.checkCertUniqueness(userCert));
	}

	/**
	 * 删除 证件操作记录表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userCertService.deleteLogic(Func.toLongList(ids)));
	}

	@GetMapping("/statistics")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "统计")
	public R<UserCertStatisticsVO> statistics() {
		return R.data(userCertService.statistics());
	}

	@PostMapping("/down/excel/template")
	@ApiOperationSupport(order = 9)
	@ApiOperation("下载excel导入模板")
	public void downExcelTemplate(HttpServletResponse response) {
		userCertService.downExcelTemplate(response);
	}

	@ApiOperation("上传导入")
	@ApiOperationSupport(order = 10)
	@PostMapping("/upload/excel")
	public R<Map<String, Object>> uploadExcel(@RequestParam("file") MultipartFile file,
											  @ApiParam(value = "是否覆盖") @RequestParam(required = false) boolean isCover) {
		Map<String, Object> resultMap = userCertService.uploadExcel(file,isCover);
		return R.data(resultMap);
	}

	@ApiOperation("下载导入错误数据")
	@ApiOperationSupport(order = 11)
	@PostMapping("/down/err/excel")
	public void downErrExcel(HttpServletResponse response) {
		userCertService.downErrExcel(response);
	}

	@ApiOperation("导出数据")
	@ApiOperationSupport(order = 12)
	@PostMapping("/export/excel")
	public void exportExcel(@RequestBody UserCertDTO userCert, HttpServletResponse response) throws IOException {
		userCertService.exportExcel(userCert, response);
	}

	@ApiOperation("待办提醒-证件到期")
	@ApiOperationSupport(order = 13)
	@GetMapping("/reminder")
	public R<Integer> reminder() {
		int count = userCertService.count(Wrappers.<UserCert>lambdaQuery()
			.in(UserCert :: getStatus, Arrays.asList(CertStatusEnum.EXPIRING_SOON.getId(), CertStatusEnum.EXPIRED.getId())));
		return R.data(count);
	}
}
